﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class handScript : MonoBehaviour
{

    private static handScript instance;
    public static handScript MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<handScript>();
            }
            return instance;
        }
    }
    private Image icon;
    public IMovable MyMovable { get; set; }

    [SerializeField]
    private Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        icon = GetComponent<Image>();
        
    }

    // Update is called once per frame
    void Update()
    {
        icon.transform.position = Input.mousePosition+offset;

        
        if (Input.GetMouseButton(0) && MyInstance.MyMovable != null && !EventSystem.current.IsPointerOverGameObject())
        {
            

            DeleteItem();

        }

    }
    public void takeMovable(IMovable move)
    {
        this.MyMovable = move;
        icon.sprite = move.MyIcon;
        icon.color = Color.white;
    }
    public IMovable put()
    {
        IMovable tmp=MyMovable;
        MyMovable = null;
        icon.color = new Color(0, 0, 0, 0);
        return tmp;
    }
    public void Drop()
    {
        MyMovable = null;
        icon.color = new Color(0, 0, 0, 0);
        inventoryScript.MyInstance.FromSlot = null;
    }
    public void DeleteItem()
    {
        Item item = MyMovable as Item;
        if (MyMovable is Item )
        {
            if (item.Slot != null)
            {
                item.Slot.Clear();
            }
            else if (item.MyCharButton != null)
            {
                item.MyCharButton.Unequip();
            }
            
            
        }

        Drop();
        inventoryScript.MyInstance.FromSlot = null;
        
    }
}
